package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author yanggld
 * @Date 2020/3/6-9:42
 */
@RestController
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

    Map<String,String> tokenInfo = new HashMap<>();
    Map<String,String> userInfo = new HashMap<>();

    @PostConstruct
    public void init(){
        tokenInfo.put("token123","123");
        tokenInfo.put("token456","456");
        userInfo.put("123","token123");
        userInfo.put("456","token456");
    }


    @CrossOrigin
    @GetMapping("/info")
    public String getInfo(@RequestParam String token){
        return tokenInfo.get(token) != null?tokenInfo.get(token) : "";
    }

    @CrossOrigin
    @GetMapping("/login")
    public String login(@RequestParam String name){
        if (name.equals("123")) {
            userInfo.put("123","token123");
            tokenInfo.put("token123","123");
        }
        return userInfo.get(name) != null?userInfo.get(name) : "";
    }

    @CrossOrigin
    @GetMapping("/logout")
    public String logout(@RequestParam String token){
        return tokenInfo.remove(token);
    }
}
